<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>用户详情</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="./css/frame/font.css">
		<link rel="stylesheet" href="./css/frame/xadmin.css">
		<link rel="stylesheet" href="./css/page/user.css" />
		<script type="text/javascript" src="./lib/jquery/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="./lib/layui/layui.js"></script>
		<script type="text/javascript" src="./js/frame/xadmin.js"></script>
		<script type="text/javascript" src="./js/constant/urls.js"></script>
		<script type="text/javascript" src="./js/utils/TimeUtils.js"></script>
	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend><b>用户信息详情</b></legend>
		</fieldset>
		<div class="main">
			<form class="layui-form" action="">
				<div class="layui-form-item">
					<div class="layui-input-block" id="roles"></div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit="" lay-filter="demo1">完成</button>
					</div>
				</div>
			</form>
		</div>
	</body>
	<script>
		$(function() {
			let loc = location.href;
			let num = loc.indexOf("id=");
			var userId = decodeURI(loc.substr(num + 3));
			$.ajax({
				type: "get",
				url: urls.sysUrl + "user/roles/" + userId,
				async: true,
				xhrFields: {
					withCredentials: true
				},
				success: function(res) {
					if (res.errorCode === 0) {
						$('#roles').html("");
						for (let role of res.data) {
							if (role.userId == null) {
								$('#roles').append("<input type='checkbox' lay-filter='checkboxTest' name='roleIds' value='" + role.id +
									"' title=" + role.name + ">");
							} else {
								$('#roles').append("<input type='checkbox' lay-filter='checkboxTest' checked='' name='roleIds' value='" +
									role.id + "' title=" + role.name + ">");
							}
						}
					}
				}
			});
			layui.use(['form', 'upload'], function() {
				var form = layui.form,
					layer = layui.layer;

				form.on('checkbox(checkboxTest)', function(data) {
					console.log(data.elem.checked); //是否被选中，true或者false
					console.log(data.value); //复选框value值，也可以通过data.elem.value得到
					$.ajax({
						type: "put",
						url: urls.sysUrl + "user/accredit/" + userId,
						async: true,
						data: {"roleId":data.value, "isAdd":data.elem.checked},
						xhrFields: {
							withCredentials: true
						},
						success: function(res) {
							if (res.errorCode === 0) {
								layer.msg(res.description, {
									icon: 6
								});
							} else {
								layer.msg(res.description, {
									icon: 5
								});
							}
						}
					});
				});
				//监听提交
				form.on('submit(demo1)', function(data) {
					// 获得frame索引
					var index = parent.layer.getFrameIndex(window.name);
					//关闭当前frame
					parent.layer.close(index);
					return false;
				});
			});
		});
	</script>
</html>
